package com.toubutu.audio.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.toubutu.audio.dto.RecordHistoryDto;
import com.toubutu.audio.entity.History;
import com.toubutu.audio.admin.mapper.HistoryMapper;
import com.toubutu.audio.dto.QueryHistoryDto;
import com.toubutu.audio.service.HistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toubutu.audio.service.VideoService;
import com.toubutu.audio.vo.HistoryVo;
import com.toubutu.audio.vo.VideoListItemVo;
import com.toubutu.common.CurrentUserContext;
import com.toubutu.common.PageUtils;
import com.toubutu.constants.BaseConstants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戒躁
 * @since 2024-02-25
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {

    @Resource
    private VideoService videoService;

    @Override
    public PageUtils<HistoryVo> getUserHistory(QueryHistoryDto dto) {
        LambdaQueryWrapper<History> wrapper = dto.getWrapper();
        Page<History> page = dto.getPage();
        page = page(page, wrapper);
        List<History> records = page.getRecords();
        if (records.isEmpty()) {
            return new PageUtils<>(Collections.emptyList(), page);
        }
        List<String> videoCodeList = new ArrayList<>();
        for (History record : records) {
            videoCodeList.add(record.getVideoCode());
        }
        Map<String, VideoListItemVo> videoInfo = videoService.getVideoItemListByVideoCode(videoCodeList).stream()
                .collect(Collectors.toMap(VideoListItemVo::getVideoCode, v -> v, (o, n) -> n));
        List<HistoryVo> result = records.stream().map(h -> {
            HistoryVo historyVo = new HistoryVo();
            historyVo.setId(h.getId());
            historyVo.setVideo(videoInfo.get(h.getVideoCode()));
            historyVo.setCreateTime(h.getCreateTime());
            historyVo.setTime(h.getTime());
            return historyVo;
        }).collect(Collectors.toList());
        return new PageUtils<>(result, page);
    }

    @Override
    public void recordHistory(RecordHistoryDto recordHistoryDto) {
        Long userId = Long.parseLong(CurrentUserContext.getUserId());
        LambdaQueryWrapper<History> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(History::getVideoCode, recordHistoryDto.getVideoCode())
                .eq(History::getUserId, userId)
                .eq(History::getIsDelete, BaseConstants.UN_DELETED);
        History history = getOne(queryWrapper);
        if (history != null) {
            LambdaUpdateWrapper<History> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(History::getUserId, userId)
                    .eq(History::getVideoCode,recordHistoryDto.getVideoCode())
                    .eq(History::getIsDelete, BaseConstants.UN_DELETED)
                    .set(History::getCreateTime,LocalDateTime.now())
                    .set(History::getTime, recordHistoryDto.getTime());
            update(updateWrapper);
            return;
        }
        History po = recordHistoryDto.toPo();
        po.setUserId(userId);
        po.setCreateTime(LocalDateTime.now());
        save(po);
    }
}
